package com.gitee.cashzhang27.test.boot.swagger.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Swagger Annotations Demo.
 *
 * @author Cash Zhang
 * @version v1.0
 * @since 2020/04/11 17:26
 */
@RestController
@AllArgsConstructor
@RequestMapping("swagger-annotations-demo")
@Api(tags = "Swagger Annotations Demo", value = "UsersController")
public class DemoController {

  @ApiResponses(@ApiResponse(code = 500, message = "算数异常"))
  @PostMapping("test1/{id}")
  @ApiOperation(value = "测试1", notes = "Swagger 注解演示")
  public void test1(
      @ApiParam(value = "ID", required = true) @PathVariable("id") String id,
      @ApiParam(value = "姓名", required = true) @RequestParam("name") String name,
      @ApiParam(value = "性别") @RequestParam(value = "sex", required = false, defaultValue = "male")
          String sex,
      @ApiIgnore(value = "保密") @RequestParam(value = "年龄", defaultValue = "male") String age,
      @ApiParam(value = "地址", example = "北京")
          @RequestParam(value = "address", required = false, defaultValue = "address")
          String address,
      @ApiParam(value = "演示", required = true) @RequestBody Demo demo) {
    int i = 1 / 0;
  }

  @ApiImplicitParams(
      @ApiImplicitParam(
          name = "name",
          value = "姓名",
          defaultValue = "张三",
          paramType = "query",
          dataType = "String",
          required = true))
  @PostMapping("test2")
  @ApiOperation("测试2")
  public void test2(HttpServletRequest request, HttpServletResponse response) {
    String name = request.getParameter("name");
    System.out.println(name);
  }
}

@Data
@ApiModel("Swagger Annotations Demo 实体")
@AllArgsConstructor
class Demo {

  @ApiModelProperty("键")
  private String key;

  @ApiModelProperty(name = "newValue", value = "值")
  private String value;
}
